﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
using System.Transactions;
namespace MBSProcessComponent
{
    public static class MBSCache
    {
        public static string SHOW_TIME_DICTIONARY = "ShowTimeDictionary";
        public static string CINEMA_DICTIONARY = "CinemaDictionary";
        public static string MOVIE_DICTIONARY = "MovieDictionary";
        public static string DATETIME_DICTIONARY = "DateTimeDictionary";

        private static Cache _cache =System.Web.HttpContext.Current.Cache;
        public static void initiateCache()
        {
            _cache[SHOW_TIME_DICTIONARY] = new Dictionary<int, Dictionary<String, String>>();
            _cache[CINEMA_DICTIONARY] = new Dictionary<int, string>();
            _cache[MOVIE_DICTIONARY] = new Dictionary<int, string>();
            if (_cache[DATETIME_DICTIONARY] == null)
            _cache[DATETIME_DICTIONARY] = new Dictionary<string, Dictionary<int, string>>();
        }

        public static bool addCinemaDictionary(Dictionary<int,string> CinemaDictionary)
        {
            _cache.Insert(CINEMA_DICTIONARY, CinemaDictionary);
            return true;
        }

        public static bool addMovieDictionary(Dictionary<int, string> MovieDictionary)
        {
            _cache.Insert(MOVIE_DICTIONARY, MovieDictionary);
            return true;
        }

        public static bool addDateTimeDictionary(Dictionary<string, Dictionary<int, string>> DateTimeDictionary)
        {
            _cache.Insert(DATETIME_DICTIONARY, DateTimeDictionary);
            return true;
        }

        public static Dictionary<int, string> getCinemaDictionary()
        {

            return (Dictionary<int, string>)_cache[CINEMA_DICTIONARY];

        }

        public static Dictionary<int, string> getMovieDictionary()
        {

            return (Dictionary<int, string>)_cache[MOVIE_DICTIONARY];

        }

        public static Dictionary<string, Dictionary<int, string>> getDateTimeDictionary()
        {

            return (Dictionary<string, Dictionary<int, string>>)_cache[DATETIME_DICTIONARY];

        }

        public static void setShowTimeDictionary(Dictionary<int, Dictionary<String, String>> ShowTimeDictionary)
        {
            _cache.Insert(SHOW_TIME_DICTIONARY, ShowTimeDictionary);
        }

        public static Dictionary<int, Dictionary<String, String>> getShowTimeDictionary()
        {
            Dictionary<int, Dictionary<String, String>> obj = _cache[SHOW_TIME_DICTIONARY] as Dictionary<int, Dictionary<String, String>>;
            return obj;
           
        }

        public static bool releaseBlockedSeats(MovieBookingSession mbs)
        {
            if (mbs != null)
            {
                Dictionary<int, Dictionary<String, String>> d = getShowTimeDictionary();
                Dictionary<String, String> showTimeSeatDictionary = d[mbs.ShowTimeId];
                List<String> selectedSeatsList = mbs.SelectedSeatsList;
                try
                {
                    foreach (String seat in selectedSeatsList)
                    {
                        showTimeSeatDictionary[seat] = "Available";
                    }
                    Dictionary<int, Dictionary<String, String>> ShowTimeDictionary = _cache[SHOW_TIME_DICTIONARY] as Dictionary<int, Dictionary<String, String>>;

                    ShowTimeDictionary[mbs.ShowTimeId] = showTimeSeatDictionary;
                    _cache.Insert(SHOW_TIME_DICTIONARY, ShowTimeDictionary);

                }
                catch (TransactionAbortedException)
                {
                    return false;
                    //Exception occured...
                }
            }
            else {
                return false;
            }
            return true;
            
        }
    }
}
